package cn.xuewenbao.manage.controller;

import cn.xuewenbao.manage.entity.IESEntity;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @Auther: mafayu
 */
@RestController
@RequestMapping("/manage/es")
@Api(tags = "后台管理es")
@Slf4j
public class IEsController {

    @Value("${elasticsearch.clientIp}")
    private String clientIp;

    @Value("${elasticsearch.port}")
    private String port;

    @Value("${elasticsearch.password}")
    private String password;

    @Value("${elasticsearch.username}")
    private String userName;

    @ApiOperation("添加文档")
    @PutMapping("addDoc/{index}")
    public Result<Boolean> addDoc(@PathVariable String index,
                                  @RequestBody IESEntity entity) throws IOException {
        Result<Boolean> result = new Result<>();

        HttpHost httpHosts = new HttpHost(clientIp, Integer.parseInt(port), "https");

        RestClientBuilder restClientBuilder = RestClient.builder(httpHosts)
                .setHttpClientConfigCallback(httpClientBuilder -> {
                    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
                    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
                    return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                });
        RestHighLevelClient client = new RestHighLevelClient(restClientBuilder);

        //插入数据DOC
        IndexRequest request = new IndexRequest();
        request.index(index).id(entity.getId());


        ObjectMapper mapper = new ObjectMapper();
        try {
            final String s = mapper.writeValueAsString(entity);
            request.source(s, XContentType.JSON);

            IndexResponse response = client.index(request, RequestOptions.DEFAULT);

            if (response.getResult() != null) {
                result.setResult(true);
            }
        } catch (JsonProcessingException e) {
            log.error(e.toString());
        }

        try {
            client.close();
        } catch (IOException e) {
            log.error(e.toString());
        }
        return result;
    }

}
